<template>
  <div class="app-container">
    <header class="app-header">
      <h1>教育视频生成器</h1>
    </header>
    
    <main class="app-main">
      <div class="input-section">
        <input 
          v-model="keyword" 
          placeholder="输入教学主题关键词" 
          @keyup.enter="generateVideo"
        >
        <button @click="generateVideo" :disabled="loading">
          {{ loading ? '生成中...' : '生成视频' }}
        </button>
      </div>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
      
      <div v-if="videoUrl" class="video-container">
        <video :src="videoUrl" controls autoplay class="preview-video"></video>
      </div>
    </main>
  </div>
</template>

<script>
import { ref } from 'vue'
import axios from 'axios'

export default {
  setup() {
    const keyword = ref('')
    const loading = ref(false)
    const error = ref('')
    const videoUrl = ref('')

    const generateVideo = async () => {
      if (!keyword.value.trim()) {
        error.value = '请输入关键词'
        return
      }
      
      try {
        loading.value = true
        error.value = ''
        const response = await axios.post('http://localhost:5000/generate', {
          keyword: keyword.value
        })
        if (response.data.status === 'success') {
          videoUrl.value = `http://localhost:5000/api/download/${response.data.video_url}`
          console.log('视频生成成功:', videoUrl.value)
        }
      } catch (err) {
        error.value = '生成失败: ' + err.message
      } finally {
        loading.value = false
      }
    }

    return { keyword, loading, error, videoUrl, generateVideo }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background-color: #f8f9fa;
  color: #333;
  line-height: 1.6;
}

.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  background: linear-gradient(135deg, #3498db, #2c3e50);
  color: white;
  text-align: center;
  padding: 2rem 0;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.app-main {
  flex: 1;
  max-width: 1200px;
  margin: 2rem auto;
  padding: 0 1.5rem;
  width: 100%;
}

.input-section {
  display: flex;
  gap: 1rem;
  margin-bottom: 2rem;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}

.input-section input {
  flex: 1;
  padding: 1rem;
  border: 2px solid #3498db;
  border-radius: 8px;
  font-size: 1.1rem;
  transition: border-color 0.3s;
}

.input-section input:focus {
  border-color: #2980b9;
  outline: none;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

.input-section button {
  background: #3498db;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0 2rem;
  font-size: 1.1rem;
  cursor: pointer;
  transition: background 0.3s;
}

.input-section button:hover {
  background: #2980b9;
}

.input-section button:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
}

.error-message {
  background: #ffebee;
  color: #c62828;
  padding: 1rem;
  border-radius: 8px;
  margin-bottom: 2rem;
  text-align: center;
}

.result-section {
  display: grid;
  grid-template-columns: 1fr;
  gap: 2rem;
  animation: fadeIn 0.5s ease-out;
}

@media (min-width: 992px) {
  .result-section {
    grid-template-columns: 1fr 1fr;
  }
}

.preview-section {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 6px 15px rgba(0,0,0,0.08);
}

.preview-video {
  width: 100%;
  display: block;
  background: #000;
}

.download-btn {
  display: block;
  text-align: center;
  background: #2ecc71;
  color: white;
  padding: 1rem;
  text-decoration: none;
  font-weight: 500;
  transition: background 0.3s;
}

.download-btn:hover {
  background: #27ae60;
}

.content-preview {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 6px 15px rgba(0,0,0,0.08);
}

.content-preview h2 {
  color: #2c3e50;
  margin-bottom: 1.5rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid #ecf0f1;
}

.knowledge-points {
  display: grid;
  gap: 1.5rem;
}

.point-card {
  background: #f8f9fa;
  border-radius: 10px;
  padding: 1.5rem;
  transition: transform 0.3s, box-shadow 0.3s;
}

.point-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0,0,0,0.1);
}

.point-card h3 {
  color: #3498db;
  margin-bottom: 0.8rem;
}

.point-card p {
  color: #555;
  margin-bottom: 1rem;
}

.point-image {
  width: 100%;
  border-radius: 8px;
  height: 180px;
  object-fit: cover;
  margin-top: 0.5rem;
}

.app-footer {
  text-align: center;
  padding: 1.5rem;
  background: #2c3e50;
  color: #ecf0f1;
  margin-top: auto;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.video-container {
  position: relative;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 6px 15px rgba(0,0,0,0.08);
}

.video-controls {
  display: flex;
  justify-content: center;
  padding: 1rem;
  background: #f8f9fa;
}

.control-btn {
  background: #3498db;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0.5rem 1.5rem;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.3s;
}

.control-btn:hover {
  background: #2980b9;
}
</style>